package com.yycx.module.bus.provider.handler.settle;

import com.yycx.common.constants.CommonConstants;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.utils.OrderNoUtil;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 结算保存扩展器
 *
 * @author zyf
 */
@Component("settlePaymentSaveHandler")
public class SettlePaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Autowired
    private BusDeptService deptService;

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private BusOrderService orderService;

    @Autowired
    private BusOrderDetailsService orderDetailsService;


    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        //结算单明细ID
        String selectIds = cs.getParams("selectIds");
        String orderId = payment.getOrderId();
        ApiAssert.isNotEmpty("结算明细不能为空", selectIds);
        Long deptId = payment.getDeptId();
        ApiAssert.isNotEmpty("deptId能为空", deptId);
        BusDept dept = deptService.getById(deptId);
        //应结算金额
        BigDecimal amount = payment.getAmount();
        Integer settleType = payment.getSettleType();
        if (FlymeUtils.isNotEmpty(selectIds)) {
            String[] ids = selectIds.split(",");
            BigDecimal settleAmount = new BigDecimal("0");
            BigDecimal yesSettleAmount = new BigDecimal("0");
            for (String id : ids) {
                BusOrderDetails orderDetails = orderDetailsService.getById(id);
                if (settleType.equals(1)) {
                    settleAmount = settleAmount.add(orderDetails.getSettleAmount());
                    yesSettleAmount=orderDetails.getSettleRealAmount();
                }
                if (settleType.equals(2))  {
                    settleAmount = settleAmount.add(orderDetails.getSettleAmount2());
                    yesSettleAmount=orderDetails.getSettleRealAmount2();
                }
                if (settleType.equals(3))  {
                    settleAmount = settleAmount.add(orderDetails.getSettleAmount3());
                    yesSettleAmount=orderDetails.getSettleRealAmount3();
                }
            }

            if (FlymeUtils.ge(yesSettleAmount,settleAmount)&&FlymeUtils.gtzero(settleAmount)) {
                ApiAssert.failure("订单已结算");
            }
        }
        BusOrder order = orderService.getById(orderId);
        payment.setCustomId(order.getCustomId().toString());
        payment.setSupplierId(order.getSupplierId());
        payment.setOrderId(orderId);
        payment.setPaymentNo("JS" + order.getOrderNo());
        payment.setTitle("结算销售费用");
        payment.setDeptId(deptId);
        payment.setSupplierRetable(amount);
        payment.setAmount(amount);
        payment.setAmountType(AmountTypeEnum.Settle.getCode());
        payment.setOrganizationId(dept.getOrganizationId());
        payment.setCompanyId(dept.getCompanyId());
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setPaymentState(CommonConstants.INT_0);
        entityMap.put("settleType", payment.getSettleType());
        entityMap.put("amount", amount);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        String selectIds = cs.getParams("selectIds");
        Integer settleType = params.getInt("settleType");
        //关联结算ID
        orderDetailsService.updateJiesuanId(entity, selectIds, settleType);
    }

}
